<?php

namespace App\Admin\Controllers;

use App\Models\Movies;
use App\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class MoviesController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Movies::class, function (Grid $grid) {

            // 第一列显示id字段，并将这一列设置为可排序列
            $grid->id('ID')->sortable();

            // // 第二列显示title字段，由于title字段名和Grid对象的title方法冲突，所以用Grid的column()方法代替
            $grid->title('名称');

            // // 第三列显示director字段，通过display($callback)方法设置这一列的显示内容为users表中对应的用户名
            $grid->directors('导演')->display(function($directors){
                return $directors[0]['name'];
            });
            // // $grid->director('导演')->display(function($userId) {
            // //     return User::find($userId)->name;
            // // });

            // // 第四列显示为describe字段
            // $grid->describe('描述');

            $grid->genres('标签')->display(function($genres){
                $g = '';
                foreach ($genres as $val) {
                    $g .= $val . ' ';
                }
                return $g;
            });

            // // 第五列显示为rate字段
            // $grid->rate('评价');

            $grid->rating('评价')->display(function($rating){
                return '评分 : ' . $rating['average'] . ' ' . $rating['stars'] . ' 人喜欢';
            });

            // // 第六列显示released字段，通过display($callback)方法来格式化显示输出
            // $grid->released('上映?')->display(function ($released) {
            //     return $released ? '是' : '否';
            // });

            // // 下面为三个时间字段的列显示
            // $grid->release_at();
            // $grid->created_at();
            // $grid->updated_at();

            // // filter($callback)方法用来设置表格的简单搜索框
            // $grid->filter(function ($filter) {

            //     // 设置created_at字段的范围查询
            //     $filter->between('created_at', 'Created Time')->datetime();
            // });

            $grid->disableExport();

            $grid->disableActions();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Movies::class, function (Form $form) {

            $userlist = User::select('id','name')->get();

            $arr = [];

            foreach ($userlist as $user) {
                $arr[$user->id] = $user->name;
            }

            $form->display('id', 'ID');

            $form->text('name','名称');

            $form->select('director','导演')->options($arr);

            $form->text('describe','描述');
            $form->text('rate','评价');

            $form->radio('released','是否上映')->options(['0' => '否', '1'=> '是'])->default('1');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
